// pages/bookDetail/bookDetail.js
import { BookModel } from '../../models/book.js'

const bookModel = new BookModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    comments: [],
    detail: null,
    isLike: false,
    likeCount: 0,
    isShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showNavigationBarLoading()
    const id = options.id
    const detail = bookModel.getDetail(id)
    const comments = bookModel.getComments(id)
    const likeStatus = bookModel.getLikeStatus(id)

    Promise.all([detail, comments, likeStatus]).then(res => {
      const [resd, resc, resl] = res
      const { comments } = resc
      const { like_status: isLike, fav_nums: likeCount } = resl

      this.setData({
        detail: resd,
        comments: comments || [],
        isLike,
        likeCount
      })
      wx.hideNavigationBarLoading()
    })

    // detail.then(res => {
    //   this.setData({
    //     detail: res
    //   })
    // })
    // comments.then(res => {
    //   this.setData({
    //     comments: res.comments || []
    //   })
    // })
    // likeStatus.then(res => {
    //   const { like_status: isLike, fav_nums: likeCount } = res
    //   this.setData({
    //     isLike,
    //     likeCount
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  handleShowChange (e) {
    const { isShow } = e.detail
    this.setData({
      isShow
    })
  },
  handleUpdateComments (e) {
    const { content } = e.detail
    this.setData({
      comments: [{
        content,
        nums: 1
      }, ...this.data.comments]
    })
  }
})